package com.hddf.project.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

/**
 * Http工具类
 * 
 * @author xxz
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class HttpUtil {
	public static InputStream getResourceAsStream(String path) {
		return ServletActionContext.getServletContext().getResourceAsStream(
				path);
	}

	public static ActionContext getActionContext() {
		return ServletActionContext.getContext();
	}

	public static ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public static HttpServletRequest getHttpServletRequest() {
		return ServletActionContext.getRequest();
	}

	public static HttpServletResponse getHttpServletResponse() {
		return ServletActionContext.getResponse();
	}

	public static HttpSession getHttpSession() {
		return ServletActionContext.getRequest().getSession(false);
	}

	public static HttpSession getHttpSession(boolean flag) {
		return ServletActionContext.getRequest().getSession(flag);
	}

	public static Map getSessionMap() {
		return getActionContext().getSession();
	}

	public static Object getSession(Object key) {
		Map map = getActionContext().getSession();
		if (map == null)
			return null;
		return map.get(key);
	}

	public static void setSession(Object key, Object value) {
		Map map = getActionContext().getSession();
		map.put(key, value);
	}

	public static void removeSession(Object key) {
		Map map = getActionContext().getSession();
		map.remove(key);
	}

	public static void removeAllSession() {
		Map map = getActionContext().getSession();
		map.clear();
		getHttpSession().invalidate();
	}

	public static String getActionName() {
		return ServletActionContext.getRequest().getRequestURI();
	}

	public static void printHttpServletResponse(Object obj) {
		PrintWriter prw = null;
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html; charset=utf-8");
			prw = response.getWriter();
			prw.print(obj);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			prw.close();
		}
	}

	public static String encodeURL(String url, String encoding) {
		try {
			return URLEncoder.encode(url, encoding);
		} catch (Exception exception) {
		}
		return url;
	}

	public static String encodePathInfo(String pathinfo) {
		String s = encodeURL(pathinfo, "UTF-8");
		char[] chars = s.toCharArray();
		StringBuffer sb = new StringBuffer();
		char c = '\000';
		for (int i = 0; i < chars.length; i++) {
			c = chars[i];
			if (c == '+')
				sb.append("%20");
			else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static String decodeURL(String url, String encoding) {
		try {
			return URLDecoder.decode(url, encoding);
		} catch (Exception exception) {
		}
		return url;
	}

	public static String getClientIP() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ret = "";
		ret = request.getHeader("x-forwarded-for");
		if (StringUtil.isEmpty(ret) || "unknown".equalsIgnoreCase(ret)) {
			ret = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtil.isEmpty(ret) || "unknown".equalsIgnoreCase(ret)) {
			ret = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtil.isEmpty(ret) || "unknown".equalsIgnoreCase(ret)) {
			ret = request.getRemoteAddr();
		}
		return ret;
	}

}
